import static java.lang.Math.abs;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

import javax.swing.JButton;
import javax.swing.JFrame;

/**
 * Frame
 * 
 * @author Zero
 *
 */
public class Frame extends JFrame {
	private static final long serialVersionUID = -1515459905698328218L;

	private static final int MIN_HEIGHT = 360;// 最小高度
	private static final int MIN_WIDTH = 650;// 最小宽度
	
	private static MenuBar menuBar;// 菜单栏
	private static Panel contentPane;// 主面板

	public Frame() {
		setUndecorated(true);// 除去装饰（标题栏和边框）
		setBounds(300, 150, 700, 400);// 设置初始位置和大小
		setBackground(new Color(0, 0, 0, 0));// 背景透明
		
		setContents();
		changeSize();
		handleButton();
		drag();
	}
	
	@Override
	public void setIconImage(Image image) {
		// TODO Auto-generated method stub
		super.setIconImage(image);
	}

	private void setContents() {
		// 菜单栏
		menuBar = new MenuBar();
		setJMenuBar(menuBar);
		// 主面板
		contentPane = new Panel();
		setContentPane(contentPane);
	}

	// 监听鼠标事件以改变 Frame 大小
	private void changeSize() {
		addMouseMotionListener(new MouseMotionAdapter() {
			// 鼠标指针位置标识
			boolean down = false;
			boolean left = false;
			boolean right = false;

			@Override
			public void mouseMoved(MouseEvent e) {
				// 鼠标指针位置判断
				if (abs(getHeight() - e.getY()) <= 3) {
					setCursor(Cursor.getPredefinedCursor(Cursor.N_RESIZE_CURSOR));
					down = true;
				} else if (e.getX() <= 3) {
					setCursor(Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR));
					left = true;
				} else if (abs(getWidth() - e.getX()) <= 3) {
					setCursor(Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR));
					right = true;
				} else {
					setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
					down = false;
					left = false;
					right = false;
				}
			}

			@Override
			public void mouseDragged(MouseEvent e) {
				Dimension dimension = getSize();
				
				if (down) {
					// 最小高度判断
					if (e.getY() <= MIN_HEIGHT) {
						dimension.setSize(dimension.getWidth(), MIN_HEIGHT);
					} else {
						dimension.setSize(dimension.getWidth(), e.getY());
					}
				} else if (left) {
					// 最小宽度判断
					if (dimension.getWidth() - e.getX() <= MIN_WIDTH) {
						dimension.setSize(MIN_WIDTH, dimension.getHeight());
					} else {
						dimension.setSize(dimension.getWidth() - e.getX(), dimension.getHeight());
						// 固定位置
						setLocation(getLocationOnScreen().x + e.getX(), getLocationOnScreen().y);
					}
				} else if (right) {
					// 最小宽度判断
					if (e.getX() <= MIN_WIDTH) {
						dimension.setSize(MIN_WIDTH, dimension.getHeight());
					} else {
						dimension.setSize(e.getX(), dimension.getHeight());
					}
				}
				setSize(dimension);
			}
		});
	}

	private void handleButton() {
		// 关闭按钮处理
		JButton btnExit = new JButton("EXIT");
		menuBar.add(btnExit);
		btnExit.setUI(new ButtonUI());
		btnExit.setCursor(new Cursor(Cursor.HAND_CURSOR));
		btnExit.setBorderPainted(false);
		btnExit.setOpaque(false);
		btnExit.setForeground(Color.white);
		btnExit.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				System.exit(0);
			}
		});

		// 最小化按钮处理
		JButton btnMix = new JButton("MIX");
		menuBar.add(btnMix);
		btnMix.setUI(new ButtonUI());
		btnMix.setCursor(new Cursor(Cursor.HAND_CURSOR));
		btnMix.setBorderPainted(false);
		btnMix.setOpaque(false);
		btnMix.setForeground(Color.white);
		btnMix.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				setExtendedState(JFrame.ICONIFIED);
			}
		});

		// 最大化按钮处理
		JButton btnMax = new JButton("MAX");
		menuBar.add(btnMax);
		btnMax.setUI(new ButtonUI());
		btnMax.setCursor(new Cursor(Cursor.HAND_CURSOR));
		btnMax.setBorderPainted(false);
		btnMax.setOpaque(false);
		btnMax.setForeground(Color.white);
		btnMax.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (getExtendedState() == JFrame.MAXIMIZED_BOTH) {
					setExtendedState(JFrame.NORMAL);
				} else {
					setExtendedState(JFrame.MAXIMIZED_BOTH);
				}
			}
		});
	}

	// 菜单栏拖拽处理
	private void drag() {
		// 为解决匿名内部类只能引用外部的final变量问题，设置元素为1的数组
		Point[] p = new Point[1];
		menuBar.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
				p[0] = e.getPoint();
			}
		});

		menuBar.addMouseMotionListener(new MouseMotionAdapter() {
			@Override
			public void mouseDragged(MouseEvent e) {
				Point p1 = e.getPoint();
				Point p2 = getLocation();
				p2.x += p1.x - p[0].x;
				p2.y += p1.y - p[0].y;
				setLocation(p2);
			}
		});
	}
}
